//
//  self.m
//  Ufan
//
//  Created by yangqingluo on 15/11/30.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "WelcomeView.h"

#define pages 4

@interface WelcomeView()<UIScrollViewDelegate>{
    UIPageControl *pageControl;
}

@end

@implementation WelcomeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        
    }
    
    return self;
}


- (void)didMoveToSuperview{
    [super didMoveToSuperview];
    
    [self buildWelcome];
}

-(void)buildWelcome{
    //显示版本新特性界面
    
    UIView *viewOfPoints = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20 * pages, 10)];
    //    self.scrollViewOfPoint = [[UIScrollView alloc]initWithFrame:self.viewOfPoints.bounds];
    UIImageView *imageViewOfRedPoint = [[UIImageView alloc]initWithFrame:CGRectMake(( pages - 1 ) * 20 + 5, 0, 10, 10)];
    [imageViewOfRedPoint setImage:[UIImage imageNamed:@"point_orange.png"]];
    UIScrollView *scrollViewOfPoint = [[UIScrollView alloc]initWithFrame:viewOfPoints.bounds];
    scrollViewOfPoint.tag = 2;
    [scrollViewOfPoint addSubview:imageViewOfRedPoint];
    [viewOfPoints addSubview:scrollViewOfPoint];
    scrollViewOfPoint.contentSize=CGSizeMake(20 * ( 2 * pages - 1 ), 10);
    scrollViewOfPoint.contentOffset=CGPointMake(20 * ( pages - 1 ), 0);
    viewOfPoints.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height - 20);
    
    
    UIScrollView *scrollViewOfPictures = [[UIScrollView alloc]initWithFrame:self.bounds];
    scrollViewOfPictures.tag = 1;
    scrollViewOfPictures.delegate = self;
    scrollViewOfPictures.pagingEnabled = YES;
    scrollViewOfPictures.bounces = NO;
    scrollViewOfPictures.showsHorizontalScrollIndicator = NO;
    scrollViewOfPictures.contentSize = CGSizeMake(self.bounds.size.width * pages, self.bounds.size.height);
    for (int i = 0; i < pages; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"welcome_%d.png",i]]];
        [scrollViewOfPictures addSubview:imageView];
        
        UIImageView *imageViewOfBlackPoint = [[UIImageView alloc]initWithFrame:CGRectMake(i * 20 + 5, 0, 10, 10)];
        [imageViewOfBlackPoint setImage:[UIImage imageNamed:@"point_black.png"]];
        [viewOfPoints insertSubview:imageViewOfBlackPoint belowSubview:scrollViewOfPoint];
    }
    
    
    UIButton *enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    enterButton.frame = CGRectMake(0, 0, 160, 40);
    [enterButton setTitle:@"立即体验" forState:UIControlStateNormal];
    [enterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //    [enterButton setBackgroundColor:[UIColor greenColor]];
    enterButton.tag = 10;
    [enterButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    enterButton.center = CGPointMake(self.bounds.size.width * (pages - 0.5), self.bounds.size.height- 60);
    [scrollViewOfPictures addSubview:enterButton];
    
    [self addSubview:scrollViewOfPictures];
    [self addSubview:viewOfPoints];
    
    pageControl = [[UIPageControl alloc]init];
    pageControl.center =  CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height - 20);
    pageControl.numberOfPages = pages;
    pageControl.currentPage = 0;
    [self addSubview:pageControl];
}

- (void)buttonClick:(id)sender {
    UIButton *button = (UIButton *)sender;
    switch (button.tag) {
            //欢迎页面_立即体验
        case 10:{
            [self removeFromSuperview];
        }
            break;
            
        default:
            break;
    }
    
    
}

#pragma UIScrollViewDelegate
/*滚动停止时，触发该函数*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //获得当前页号
    //    int page = floor((_scrollViewBody.contentOffset.x - 160) / 320) + 1;
    
}
/* 触摸屏幕来滚动画面还是其他的方法使得画面滚动，皆触发该函数*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //    NSLog(@"scrollViewDidScroll:%f-%f",scrollView.contentOffset.x,scrollView.contentSize.width);
    if (scrollView.tag == 1){
        ((UIScrollView *)[self viewWithTag:2]).contentOffset = CGPointMake(20 *(pages - 1) - 20 * scrollView.contentOffset.x / scrollView.bounds.size.width, 0);
        
        int page = floor((scrollView.contentOffset.x - 0.5 * scrollView.bounds.size.width) / scrollView.bounds.size.width) + 1;
        pageControl.currentPage = page;
    }
}



@end
